﻿using System.Collections.Generic;
using System.Web.UI;
using viBlog.Controllers;
using visinia.SmartEngine;
using System.Web;

namespace viBlog.Classes
{
    /// <summary>
    /// The base class for the post view controls
    /// </summary>
    public class PostView : UserControl
    {
        private bool _ShowFullPost = false;
        public bool ShowFullPost
        {
            get { return _ShowFullPost; }
            set { _ShowFullPost = value; }
        }

        /// <summary>
        /// Display ads between the summary and description
        /// </summary>
        private bool _ShowAds = false;
        public bool ShowAds
        {
            get { return _ShowAds; }
            set { _ShowAds = value; }
        }
        private string _Ads;
        public string Ads
        {
            get {
                if (_ShowAds)
                    return _Ads;
                else
                    return null;
                }
            set { _Ads = value; }
        }

        private Post _Post;
        public Post Post
        {
            get { return _Post;  }
            set { _Post = value; }
        }
        
        public string Description
        {
            get
            {
                if (_ShowFullPost)
                    return PostController.GetPostDescription(Post.PostId);
                else
                    return null;
            }
        }

        public Post PreviousPost
        {
            get{
                if (_ShowFullPost)
                    return PostController.GetPost(PostController.GetPostRelatives(Post.PostId).PreviousPostId);
                else
                    return null;
            }
        }

        public Post NextPost
        {
            get{
                if (_ShowFullPost)
                    return PostController.GetPost(PostController.GetPostRelatives(Post.PostId).NextPostId);
                else
                    return null;
            }
        }

        public Post FirstPost
        {
            get{
                if (_ShowFullPost)
                    return PostController.GetPost(PostController.GetPostRelatives(Post.PostId).FirstPostId);
                else
                    return null;
            }
        }

        public Post LastPost
        {
            get{
                if (_ShowFullPost)
                    return PostController.GetPost(PostController.GetPostRelatives(Post.PostId).LastPostId);
                else
                    return null;
            }
        }

        public int GetCommentsCount()
        {
            return CommentController.GetCommentsCount(Post.PostId);
        }

        public string GetCategories()
        {
            List<Category> categories = CategoryController.GetCategories(Post.PostId);            
            if (categories.Count == 0)
                return null;            
            string CategoryLink = "<a href=\"{0}\" rel=\"category\">{1}</a>";
            List<string> Categorylinks = new List<string>();
            foreach (Category category in categories)
            {
                Categorylinks.Add(string.Format(CategoryLink, WebRoots.GetWebRoot + "Blog/Category/" + HttpUtility.UrlEncode(category.Name) + ".aspx", category.Name));
            }
            return string.Join(", ", Categorylinks.ToArray()); 
        }

        public string GetTags()
        {
            List<Tag> tags = TagController.GetTags(Post.PostId);
            if(tags.Count == 0)            
                return null;
            string TagLink = "<a href=\"{0}\" rel=\"tag\">{1}</a>";
            List<string> taglinks = new List<string>();
            foreach (Tag tag in tags)
            {
                taglinks.Add(string.Format(TagLink, WebRoots.GetWebRoot + "Blog/Tag/" + HttpUtility.UrlEncode(tag.Name) + ".aspx", tag.Name));
            }
            return string.Join(", ", taglinks.ToArray());            
        }
    }
}
